package com.light.gamecomponents.physics;

import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.light.gamecomponents.gravitylight.GravityLight;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class DeathGear extends PhysicsComponents implements ContactListener{
	
	private Sound m_deathSound;
	
	public DeathGear(Roller game, String sName,Vector2 initialPosition, Vector2 size) 
	{
		super(game, sName, initialPosition, size);
	}
	
	private LoopEntityModifier m_nowModifiler;

	@Override
	public void onUpdate(float pSecondsElapsed) {
		
	}

	@Override
	public void reset() {
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources(m_resourceName, m_game);
		m_textureRegion = ResourceManager.getTextureRegion(m_resourceName);
		ResourceManager.onLoadSoundResources("KillByJigsaw", m_game);
		m_deathSound = ResourceManager.getSound("KillByJigsaw");	
	}
	
	@Override  
	public void unLoadResources() {
		m_textureRegion = null;
		ResourceManager.unLoadResources(m_resourceName, m_game);
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {
		FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(30, 0.0f, 1.0f);
		
		objectFixtureDef.filter.categoryBits = Roller.CATEGORYBIT_NORMAL;
		objectFixtureDef.filter.maskBits = Roller.MASKBIT_NORMAL;
		
		m_sprite = new Sprite(m_initialPosition.x, 
				  		      m_initialPosition.y,
                              m_textureRegion);
		
		
		
		m_body = PhysicsFactory.createCircleBody(physicsWorld, 
											  m_sprite, 
											  BodyType.DynamicBody, 
											  objectFixtureDef);
		

		scene.attachChild(m_sprite);
		
		scene.registerUpdateHandler(this);
		
		m_physicsConnector = new PhysicsConnector(m_sprite, m_body, true, false);
		physicsWorld.registerPhysicsConnector(m_physicsConnector);
		((RollerPhysicsWorld)physicsWorld).registerContactListener(this);
		
		m_body.setType(BodyType.StaticBody);
		m_body.setLinearVelocity(new Vector2(0.0f, 0.0f));
		m_body.setAngularVelocity(0);
	
	}	

	
	public void stopBody() 
	{ 
		if(m_nowModifiler!=null)m_sprite.unregisterEntityModifier(m_nowModifiler);
		m_nowModifiler = null;
	}
	public void startBody() 
	{ 
		if(m_nowModifiler==null)
		{
			m_nowModifiler = new LoopEntityModifier(99999999, new SequenceEntityModifier(new RotationModifier(0.7f, m_sprite.getRotation(), m_sprite.getRotation()+359)));
			m_sprite.registerEntityModifier(m_nowModifiler);
		}
	}
	
	
	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		physicsWorld.unregisterPhysicsConnector(m_physicsConnector);
		scene.detachChild(m_sprite);
		physicsWorld.destroyBody(m_body);		
	
	}

	@Override
	public void onLoadEngine() {
		
	}

	@Override
	public void setPosition(int x, int y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beginContact(Contact contact) {
		if ((contact.getFixtureA().getBody() == this.m_body || contact.getFixtureB().getBody() == this.m_body)
				&&
				(contact.getFixtureA().getBody() == Roller.player.getBody() || contact.getFixtureB().getBody() == Roller.player.getBody()))
				{
					Roller.player.kill();
					if (Roller.playMusic)
						m_deathSound.play();
				}
	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}	
	
	public boolean isInLight(GravityLight light)
	{
		Vector2 disVec = new Vector2(getSpritePosition().x - light.getSpritePosition().x, 
				 					 getSpritePosition().y - light.getSpritePosition().y);

		float dis = (float)Math.sqrt(disVec.x * disVec.x + disVec.y * disVec.y);
		float minDis = m_sprite.getWidth() / 2 + light.getWidth() / 2 - 20;
		
		if (dis <= minDis)
			return true;
		else
			return false;
		
	}
}